package com.springboot.uav.service.impl;

import com.springboot.uav.entity.Feedback;
import com.springboot.uav.dto.FeedbackDto;
import com.springboot.uav.mapper.FeedbackMapper;
import com.springboot.uav.service.FeedbackService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public boolean submitFeedback(FeedbackDto feedbackDto) {
        Feedback feedback = new Feedback();
        feedback.setUserId(feedbackDto.getUserId());
        feedback.setMessage(feedbackDto.getMessage());
        feedback.setCreatedAt(LocalDateTime.now());
        feedback.setStatus("未处理");
        return feedbackMapper.insert(feedback) > 0;
    }

    @Override
    public List<Feedback> getFeedbackHistory(Integer userId) {
        return feedbackMapper.selectList(new QueryWrapper<Feedback>().eq("user_id", userId));
    }

    @Override
    public boolean updateFeedbackStatus(Long id, String status) {
        Feedback feedback = feedbackMapper.selectById(id);
        if (feedback == null) {
            return false;
        }
        feedback.setStatus(status);
        return feedbackMapper.updateById(feedback) > 0;
    }

    @Override
    public boolean addFeedbackResponse(Long id, String response) {
        Feedback feedback = feedbackMapper.selectById(id);
        if (feedback == null) {
            return false;
        }
        feedback.setResponse(response);
        feedback.setStatus("已处理");
        return feedbackMapper.updateById(feedback) > 0;
    }

    @Override
    public Page<Feedback> getAllFeedbacks(int page, int size) {
        return feedbackMapper.selectPage(new Page<>(page, size), null);
    }
}
